<?php

namespace wangdiantong;
/**
 * Created by PhpStorm.
 * User: linyu
 * Date: 2019/08/1
 */
class Wdtwms
{
    public $elementLevel = 0;
    private $app_key = 'wdt16279827837876';
    private $app_secret = '504a7ee32f55029110e3d73efd4f5b2d';
    private $ownerCode = 'wms_lt_xpxgWDT';
    private $sid = 'wms_lt_xpxgWDT';
    private $warehouseCode = 'wms_lt_xpxgWDT';
    private $url = 'http://39.105.14.222/wms_api/wdt_service.php';

    /**
     * @param $appsecret
     * @param $params
     * @param $content 请求体参数
     * @return string
     */
    public function MakeSign($appsecret, &$params, &$content)
    {
        $str = $appsecret;
        ksort($params);
        foreach ($params as $key => $value) {
            if ('sign' == $key) {
                continue;
            }
            if (!empty($key) && !empty($value))
                $str .= $key . $value;
        }

        $str .= $content;
        $str .= $appsecret;
        // 对接trade.weight等定制接口（接口名字一般是小写）不用去除空白字符
        // 对接通用推送接口、通用回传接口才去除空白
        $str = str_replace(array("\t", "\r", "\n", " "), "", $str);  // 去除空白字符
//        echo "加密前参数\t" . $str;
//        echo PHP_EOL;
//        echo PHP_EOL;

        $md5 = strtoupper(md5($str));
//        echo "加密后\t" . $md5;
//        echo PHP_EOL;
//        echo PHP_EOL;
        return $md5;
    }

    /**
     * 商品同步接口
     * @param $method
     * @param $content
     * @param $sid
     * @param $app_key
     * @param $app_secret
     * @param $url
     * @return false|string
     */
    public function wdt_api(&$content, $method)
    {
        if (isset($content['ownerCode'])) {
            $content['ownerCode'] = $this->ownerCode;
        }
        //仓库编码
        if (isset($content['entryOrder']['warehouseCode'])) {
            $content['entryOrder']['warehouseCode'] = $this->ownerCode;
        }//仓库编码
        if (isset($content['deliveryOrder']['warehouseCode'])) {
            $content['deliveryOrder']['warehouseCode'] = $this->warehouseCode;
        }
        date_default_timezone_set('PRC');
//        var_dump($content);die;
//        var_dump($content);die;
        $res = $this->arr_to_xml($content);
        $content = $res;
        $params = array(
            'sid' => $this->sid,
            'appkey' => $this->app_key,
            'appsecret' => $this->app_secret,
            'sign_method' => 'md5',
            'format' => 'xml',
            'timestamp' => date('Y-m-dH:i:s'),
            'method' => $method,
        );
        $params['sign'] = $this->MakeSign($this->app_secret, $params, $content);
        $opts = array(
            'http' => array(
                'method' => 'POST',
                'header' => "Content-Type: text/xml\r\nContent-Length: " . strlen($res) . "\r\n",
                'content' => $res
            )
        );
        $context = stream_context_create($opts);
        $service_url = $this->url . '?' . http_build_query($params);
//        echo $res.PHP_EOL.PHP_EOL;

//        echo "请求地址:\t". $service_url.PHP_EOL;
        $response = file_get_contents($service_url, 0, $context);
//        echo($response);
//        echo $params['sign'];
//        die;
        return $this->xmlArray($response);
    }


    public function arr_to_xml($data, $root = true)
    {
//        var_dump($data);die;
        $str = "";
        if ($root) {
            $str .= '<?xml version="1.0" encoding="utf-8"?>';
            $str .= "<request>";
        }
        foreach ($data as $key => $val) {
            if (is_array($val)) {
                $child = $this->arr_to_xml($val, false);
                if (is_numeric($key)) {

                    $keys = $val['key'];
                    $str .= "<$keys>$child</$keys>";

                } else {
                    if ($key != 'key') {
                        $str .= "<$key>$child</$key>";
                    }
                }
            } else {
                if ($val == '') {
                    $str .= "<$key/>";

                } else {
                    if ($key != 'key') {
                        $str .= "<$key>$val</$key>";
                    }
                }
            }
        }
        if ($root) {
            $str .= "</request>";

        }

        return $str;
    }


    /**
     * xml转数组
     */
    public function xmlArray($data)
    {
        return json_decode(json_encode(simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }

}